package com.inch.web.controller;

import com.inch.web.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * query:   all users
 *          user with a id
 * delete:  a user with id=xx
 * add:     a user to all users
 * update:  a user to all users
 *
 * source: all users: /users
 *         a user: /users/{id}
 */
@RestController
public class UserController {
    @GetMapping("/users")
    public List<User> queryAllUser(){
        System.out.println("queryAllUser with get");
        User user1= new User(); User user2 = new User();
        user1.setId(1234); user1.setName("spring"); user1.setGender(true);user1.setBirth(new Date());
        user2.setId(4567); user2.setName("json"); user2.setGender(false);user2.setBirth(new Date());
        return Arrays.asList(user1,user2);
    }

    @PostMapping("/users")
    public String addUser(@RequestBody User user){
        System.out.println("User added" + user);
        return "User added:" + user;
    }

    @PutMapping("/users")
    public String updateUser(@RequestBody User user)  {
        System.out.println("User updated" + user);
        return "update successful:" + user;
    }

    @GetMapping("/users/{id}")
    public User queryUserWithID(@PathVariable("id") int id){
        System.out.println("query a user with id:" + id);
        User user = new User();
        user.setId(id); user.setName("mybatis"); user.setGender(true); user.setBirth(new Date());
        return user;
    }

    @DeleteMapping("/users/{id}")
    public String deleteUserWithID(@PathVariable("id") int id){
        System.out.println("User deleted with id:" + id);
        return "Delete successful with id" + id;
    }



}
